<template>
<div class="articaldetail">
    <div class="header">
        <div class="left">
            <van-icon name="arrow-left back"
                      @click="$router.back()" />
            <span class="iconfont iconnew new"></span>
        </div>
        <!-- 动态设置关注按钮的样式
        未关注：显示红色背景
        已关注：显示白色背景
        后台会返回一个has_follow字段给我们判断是否关注用户
            false表示未关注，true表示已关注
         -->
        <span :class="{active: arDetail.has_follow}" 
        @click="followFn">关注</span>
    </div>
    <div class="detail">
        <div class="title">{{arDetail.title}}</div>
        <div class="desc">
            <span>{{arDetail.user&&arDetail.user.nickname}}</span> &nbsp;&nbsp;
            <span>{{ new Date().toLocaleDateString() }}</span>
        </div>
        <!-- 
            文章类型：
                视频：type=2
                非视频：type=1
         -->
        <div v-if="arDetail.type==1" v-html="arDetail.content" class="content">
            <!-- {{ arDetail.content }} -->
        </div>
        <!-- 视频 -->
        <div v-if="arDetail.type==2" class="content">
            <!-- 
                poster: 设置视频封面
                controls：视频控制器
             -->
            <video width="100%" :poster="arDetail.cover[0].url|joinPath" controls :src="arDetail.content"></video>
        </div>
        <div class="opt">
            <span class="like">
                <van-icon name="good-job-o" />点赞
            </span>
            <span class="chat">
                <van-icon name="chat"
                          class="w" />微信
            </span>
        </div>
    </div>
    <!-- 精彩跟帖 -->
    <div class="keeps">
        <h2>精彩跟帖</h2>
        <div class="item">
            <div class="head">
                <img src="../assets/风铃玉秀.jpg"
                     alt />
                <div>
                    <p>火星网友</p>
                    <span>2小时前</span>
                </div>
                <span>回复</span>
            </div>
            <div class="text">文章说得很有道理</div>
        </div>
        <div class="more">更多跟帖</div>
    </div>
    <!-- 底部评论块 -->
    <commentFooter :obj="arDetail"></commentFooter>
</div>
</template>

<script>
import commentFooter from "@/components/commentFooter.vue"
import { articleDetail } from "@/api/news.js"
import { user_follows, user_unfollw } from "@/api/user.js"
export default {
    components: { commentFooter },
    data(){
        return {
            arDetail: {}, // 文章详情
        }
    },
    created(){
        // 获取文章id
        let id = this.$route.query.id;
        console.log("id获取成功",id);
        // 获取文章详情数据
        articleDetail(id).then(res=>{
            console.log(64,res);
            this.arDetail = res.data.data;
        })
    },
    methods: {
        // 关注用户
        followFn(){
            // 当has_follow为true的时候，表示此刻已经关注过用户了
            // 所以此刻应该取消关注
            if(this.arDetail.has_follow){
                // 实现取消关注功能
                user_unfollw(this.arDetail.user.id).then(res=>{
                    console.log(99,res);
                    if(res.data.message == "取消关注成功"){
                        // 更新页面效果
                        this.arDetail.has_follow = false;
                        this.$toast.success(res.data.message);
                        return
                    }
                    this.$toast.fail(res.data.message);
                })
            }else{
                // 调用接口函数，实现关注功能
                user_follows(this.arDetail.user.id).then(res=>{
                    // console.log(97,res);
                    if(res.data.message == "关注成功"){
                        // 更新页面效果
                        this.arDetail.has_follow = true;
                        this.$toast.success(res.data.message);
                        return
                    }
                    this.$toast.fail(res.data.message);
                })
            }

            
        }
    }
}
</script>

<style lang="less" scoped>
.header {
  padding: 0px 10px;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ccc;
  > .left {
    height: 100%;
    vertical-align: middle;
    position: relative;
    .van-icon {
      position: absolute;
      top: 15px;
      left: 0;
    }
    > span {
      margin-left: 22px;
      font-size: 50px;
    }
  }
  > span {
    padding: 5px 15px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 15px;
    font-size: 13px;
    // &表示span标签
    &.active{
        background-color: #fff;
        color: #000;
        border: 1px solid #000;
    }
  }
}
.detail {
  padding: 15px;
  .title {
    font-size: 18px;
    font-weight: bold;
    padding: 10px 0;
  }
  .desc {
    line-height: 30px;
    color: #999;
    font-size: 13px;
  }
  /deep/.content {
    text-indent: 2em;
    line-height: 24px;
    font-size: 15px;
    padding-bottom: 30px;
    width: 100%;
    // 设置v-html渲染的标签样式需要通过样式穿透才能设置
    .photo{
        text-indent: 0;
        img{
            width: 100%;
        }
    }
    a{
        display: block;
        text-indent: 0;
        img {
            width: 100%;
        }
    }
  }
}
.opt {
  display: flex;
  justify-content: space-around;
  .like,
  .chat {
    height: 25px;
    padding: 0 15px;
    font-size: 14px;
    line-height: 25px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
  }
  .w {
    color: rgb(84, 163, 5);
  }
}
.keeps {
  border-top: 5px solid #ddd;
  padding: 0 15px;
  > h2 {
    line-height: 50px;
    text-align: center;
  }
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: (50/360) * 100vw;
        height: (50/360) * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
  .more {
    width: 100px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
    margin: 20px auto;
    font-size: 13px;
  }
}
</style>